<?php

include_once dirname(dirname(__FILE__)) . '/include/common.php';
include_once dirname(dirname(__FILE__)) . '/include/xmppChat.php';

class cronjobInviteGuest extends core {

    public function process($event_id, $access_token, $domain) {
        $messages_list = $this->db->get_results('SELECT igq.invite_id, igq.invite_message_id,
                                                        igq.guest_email,igq.guest_id,
                                                        igq.fb_guest_id,igq.fb_user_id,
                                                        igm.subject, igm.content, igm.event_id
	        				FROM invite_guest_messages igm,
                                                     invite_guest_queue igq   
	        				WHERE igq.invite_message_id=igm.invite_message_id
                                                AND checked=0 AND igm.event_id =' . $event_id . '
	        				ORDER BY invite_id asc LIMIT 0,5000');
        if ($messages_list) {
            //Update checked field => tránh trường hợp đang gửi mail thì server lỗi hoặc đang gửi thì 1 threat khác chạy lại file làm 1 địa chỉ mail có thể được gửi nhiều lần
            $list_ids = '';
            foreach ($messages_list as $msg) {
                $list_ids .= $msg->request_id . ', ';
            }
            $list_ids = substr($list_ids, 0, -2);
            $this->db->query('UPDATE invite_guest_queue
                        SET checked=1
                        WHERE invite_id IN (' . $list_ids . ')');

            //delete record after sent mail
            foreach ($messages_list as $msg) {
                $current_user =$this->db->get_row("SELECT first_name,last_name FROM users WHERE id= (SELECT user_id FROM events WHERE event_id=" . $msg->event_id.")");
                $user_name = $current_user->first_name." ".$current_user->last_name;
                if ($msg->guest_email != "") {
                    $data_event = $this->db->get_row("SELECT * FROM events WHERE event_id=" . $msg->event_id);
                    $encode_arg_yes = base64_encode($msg->event_id . "-" . $msg->guest_id . "-1");
                    $encode_arg_maybe = base64_encode($msg->event_id . "-" . $msg->guest_id . "-0");
                    $encode_arg_no = base64_encode($msg->event_id . "-" . $msg->guest_id . "-2");
                    $event_name = $data_event->title;
                    $event_des= $data_event->description;
                    if($event_des!="" && $event_des!= null){
                        $html_event_des ='<tr><td valign="top" style="padding:0 1em 10px 0;color:#888;white-space:nowrap"><div><i style="font-style:normal">Event description</i></div></td><td valign="top" style="padding-bottom:10px;color:#222">' .$event_des . '</td></tr>';
                    }else{
                        $html_event_des='';
                    }
                    $user = $this->db->get_row("SELECT email FROM users WHERE id=" . $data_event->user_id);
                    $sent_by = $user->email;
                    if ($data_event->end_time == $data_event->start_date || $data_event->end_time == "0000-00-00 00:00:00") {
                        $end_time = "";
                    } else {
                        $end_time = " &ndash; ".date('m-d-Y h:i A', strtotime($data_event->end_time));
                    }
                    $messages = '<table cellspacing="0" cellpadding="8" border="0"  style="font-size:14px;width:100%;font-family:Arial,Sans-serif;border-width:1px 2px 2px 1px;border:1px Solid #ccc" ><tr><td><div style="padding:2px">                                         <div style="padding-bottom:15px;color:#222;white-space:pre-wrap!important;white-space:-moz-pre-wrap!important;white-space:-pre-wrap!important;white-space:-o-pre-wrap!important;white-space:pre-wrap;word-wrap:break-word">' . $msg->content . '</div><table cellspacing="0" cellpadding="0" border="0" ><tr><td valign="top" style="padding:0 1em 10px 0;color:#888;white-space:nowrap"><div><i style="font-style:normal">Event title</i></div></td><td valign="top" style="padding-bottom:10px;color:#222">' .$event_name . '</td></tr>'.$html_event_des.'<tr><td valign="top" style="padding:0 1em 10px 0;color:#888;white-space:nowrap"><div><i style="font-style:normal">When</i></div></td><td valign="top" style="padding-bottom:10px;color:#222">' . date('m-d-Y h:i A', strtotime($data_event->start_time))  . $end_time . '</td></tr><tr><td valign="top" style="padding:0 1em 10px 0;color:#888;white-space:nowrap"><div><i style="font-style:normal">Where</i></div></td><td valign="top" style="padding-bottom:10px;color:#222"><span><span>' . $data_event->location . '</span></span></td></tr></table></div><p style="color:#222;margin:0"><span style="color:#888">Join?&nbsp;&nbsp;&nbsp;</span><strong><a style="color:#20c;white-space:nowrap" href="' . $domain . '/reply_invitation.php?arg=' . $encode_arg_yes . '">Yes</a><span style="margin:0 0.4em;font-weight:normal"> - </span><a  style="color:#20c;white-space:nowrap" href="' . $domain . '/reply_invitation.php?arg=' . $encode_arg_maybe . '">Maybe</a><span style="margin:0 0.4em;font-weight:normal"> - </span><a  style="color:#20c;white-space:nowrap" href="' . $domain . '/reply_invitation.php?arg=' . $encode_arg_no . '">No</a></strong></p><p>Sent by: '.$user_name.' ('.$sent_by.') </p></td></tr></table>';
                    if (core::send_mail($msg->guest_email, $msg->subject, $messages)) {
                        $this->db->query('DELETE FROM invite_guest_queue
                        WHERE invite_id = ' . $msg->invite_id);
                    }
                    //Check complete sent email
                    $check_complete = $this->db->get_results('SELECT * FROM invite_guest_queue WHERE invite_message_id=' . $msg->invite_message_id . ' LIMIT 1');
                    if (empty($check_complete)) {//Đã xử lý hết trong queue
                        $this->db->query('UPDATE invite_guest_messages SET is_sent=1 where invite_message_id=' . $msg->invite_message_id);
                    }
                } else {
                    $data_event = $this->db->get_row("SELECT * FROM events WHERE event_id=" . $msg->event_id);
                    $encode_arg_yes = base64_encode($msg->event_id . "-" . $msg->guest_id . "-1");
                    $encode_arg_maybe = base64_encode($msg->event_id . "-" . $msg->guest_id . "-0");
                    $encode_arg_no = base64_encode($msg->event_id . "-" . $msg->guest_id . "-2");
                    if ($data_event->end_time == $data_event->start_date || $data_event->end_time == "0000-00-00 00:00:00") {
                        $end_time = "";
                    } else {
                        $end_time = " &ndash; ".date('m-d-Y h:i A', strtotime($data_event->end_time));
                    }
                     $event_name = $data_event->title;
                    $event_des= $data_event->description;
                     $user = $this->db->get_row("SELECT email FROM users WHERE id=" . $data_event->user_id);
                    $sent_by = $user->email;
                    $messages = $msg->subject . '. ' . $msg->content . '. Event title: '.$event_name.'. Event description: '.$event_des.'. When: ' . date('m-d-Y h:i A', strtotime($data_event->start_time)) .  $end_time . '. Where: ' . $data_event->location . '. Join:  Yes - ' . $domain . '/reply_invitation.php?arg=' . $encode_arg_yes . '. Maybe - ' . $domain . '/reply_invitation.php?arg=' . $encode_arg_maybe . ' . No - ' . $domain . '/reply_invitation.php?arg=' . $encode_arg_no.". Sent by: ".$user_name." (".$sent_by.")";
                    send_message_chat($access_token, $msg->fb_user_id, $msg->fb_guest_id, $messages);
                    $this->db->query('DELETE FROM invite_guest_queue
                        WHERE invite_id = ' . $msg->invite_id);

                    //Check complete sent email
                    $check_complete = $this->db->get_results('SELECT * FROM invite_guest_queue WHERE invite_message_id=' . $msg->invite_message_id . ' LIMIT 1');
                    if (empty($check_complete)) {//Đã xử lý hết trong queue
                        $this->db->query('UPDATE invite_guest_messages SET is_sent=1 where invite_message_id=' . $msg->invite_message_id);
                    }
                }
            }
        }
    }

}

$event_id = $argv[1];
$access_token = $argv[2];
$domain = $argv[3];
$processor = new cronjobInviteGuest();
$processor->process($event_id, $access_token, $domain);
echo "Done_email<br/>";
?>